﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class api_script_redeemCodeList : ScriptClass
{
    redeemCodeList bll = new redeemCodeList();
    protected void Page_Load(object sender, EventArgs e)
    {
        string action = Request["action"];
        switch (action)
        {
            case "exchange": // 兑换
                lock (PageBase.get2)//线程锁
                {
                    exchange();
                }
                break;
            case "checkCode": // 检测兑换码是否存在
                checkCode();
                break;
            default:
                ResponseData rd = new ResponseData();
                rd.code = 0;
                rd.data = null;
                rd.message = "读写参数错误";
                Json(rd);
                break;
        }
    }

    private void checkCode()
    {
        ResponseData rd = new ResponseData();
        try
        {
            string gid = Request["gid"];
            string code = Request["code"];

            if (gid == null || code == null)
            {
                Response.Write("异常：缺少参数");
                return;
            }
            string sql = "select * from redeemCodeList where isUse = '0' and code = @code and gid = @gid";
            Hashtable h_sel = new Hashtable();
            h_sel["code"] = code;
            h_sel["gid"] = gid;
            DataTable dt = new SQLHelper("getnum").ExcuteQuery(sql, h_sel);
            if (dt.Rows.Count > 0)
            {
                rd.code = 1;
                rd.data = null;
                rd.message = "可以使用";
            }
            else
            {
                rd.code = -1;
                rd.message = "兑换码不存在或已使用";
                rd.data = null;
            }
        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦，错误详情=> " + ex.Message;
        }
        Json(rd);
    }

    private void exchange()
    {
        ResponseData rd = new ResponseData();
        try
        {
            string gid = Request["gid"];
            string code = Request["code"];
            string deviceType = Request["deviceType"];
            string topupAccount = Request["topupAccount"];

            if (gid == null || code == null || deviceType == null || topupAccount == null)
            {
                Response.Write("异常：缺少参数！请检查 ");
                return;
            }

            // 更新使用状态
            string sql = "update redeemCodeList set isUse = '1', useTime = CONVERT(varchar(100), GETDATE(), 120), topupAccount = @topupAccount " +
                "where isUse = '0' and code = @code and gid = @gid";
            Hashtable h_upd = new Hashtable();
            h_upd["code"] = code;
            h_upd["gid"] = gid;
            h_upd["topupAccount"] = topupAccount;
            int i_upd = new SQLHelper("getnum").Execute(sql, h_upd);
            if (i_upd > 0)
            {
                 // 新增充值记录
                sql = "insert into rechargeRecord(gid,code,topupAccount,accountType,gameArea,deviceType,createTime) " +
                    "values(@gid, @code, @topupAccount, '', '', @deviceType, CONVERT(varchar(100), GETDATE(), 120));";
                h_upd["deviceType"] = deviceType;
                int i_insLog = new SQLHelper("getnum").Execute(sql, h_upd);
                rd.code = 1;
                rd.data = null;
                rd.message = "获取成功";
            }
            else
            {
                rd.code = -1;
                rd.message = "兑换码/激活码使用失败, 可能不存在";
                rd.data = null;
            }
        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦，错误详情=> " + ex.Message;
        }
        Json(rd);
    }

}